/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: sample_ptr.h
 *
 * Purpose: The Communication Management shall provide a SamplePtr template 
 *          which provides a pointer to a managed data object. The 
 *          implementation shall at least contain the following constructors,
 *          assign operators and methods
 *
 * Developer:
 *   wen.gu , 2020-04-21
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_COM_SAMPLE_PTR_H__
#define __ARA_COM_SAMPLE_PTR_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include <memory>

#include "ara/com/e2e/e2e_types.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace com
{

/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

template< typename T >
class SamplePtr 
{
    using check_status = ara::com::e2e::ProfileCheckStatus;

public:
    // Default constructor
    constexpr SamplePtr() noexcept
    {
        /** todo something */
    }
    // semantically equivalent to Default constructor
    constexpr SamplePtr(nullptr_t) noexcept
    {
        /** todo something */
    }
    // Copy constructor is deleted
    SamplePtr(const SamplePtr&) = delete;
    // Move constructor
    SamplePtr(SamplePtr&& other) noexcept
        :mCheckStatus(other.mCheckStatus),
        mDataPtr(std::move(other.mDataPtr))
    {
        /** todo something */
    }

public:
    // Default copy assignment operator is deleted
    SamplePtr& operator=(const SamplePtr&) = delete;
    // Assignment of nullptr_t
    SamplePtr& operator=(nullptr_t) noexcept
    {
        mCheckStatus = check_status::kOk; /** todo refine me? */
        mDataPtr.reset(nullptr_t);

        return *this;
    }
    // Move assignment operator
    SamplePtr& operator=(SamplePtr&& other) noexcept
    {
        mCheckStatus = other.mCheckStatus;
        mDataPtr = std::move(other.mDataPtr);
        return *this;
    }
public:
    // Dereferences the stored pointer
    T& operator*() const noexcept
    {
        return *mDataPtr;
    }
    T* operator->() const noexcept
    {
        return mDataPtr.get();
    }
    //Checks if the stored pointer is null
    explicit operator bool() const noexcept
    {
        return mDataPtr.operator bool();
    }
    // Swaps the managed object
    void Swap(SamplePtr& other) noexcept
    {
        check_status cs = mCheckStatus;
        mCheckStatus = other.mCheckStatus;
        other.mCheckStatus = cs;
        mDataPtr.swap(other.mDataPtr);
    }
    //Replaces the managed object
    void Reset(nullptr_t)
    {
        mCheckStatus = check_status::kOk; /** todo refine me? */
        mDataPtr.reset(nullptr_t);
    }

    //Returns the stored object
    T* Get() const noexcept
    {
        return mDataPtr.get();
    }
    // Returns the end 2 end protection check result
    check_status GetProfileCheckStatus() const noexcept
    {
        return  mCheckStatus;
    }

private:
    check_status mCheckStatus;
    std::shared_ptr<T> mDataPtr;
};

} /** namespace com */
} /** namespace ara */

#endif /** !__ARA_COM_SAMPLE_PTR_H__ */

